<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 商品管理
                </el-breadcrumb-item>
                <el-button style="margin-left: 90%"
                           type="text"
                           icon="el-icon-refresh"
                           @click="getData"
                >刷新
                </el-button>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除
                </el-button>
                <el-select @change="selectCategory"
                           @click.native="getDivision()"
                           v-model="value" size="small"
                           placeholder="请选择分类" style="width: 12%">
                    <el-option v-for="val in selectName" :key="val.id" :value="val.id" :label="val.categoryName" />
                </el-select>
                <el-input size="small" style="margin-right: 5px;margin-left:5px;width: 12%" prefix-icon="el-icon-search"
                          v-model="query.productName" placeholder="请输入名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-circle-plus" @click="handleAdd">添加商品</el-button>
                <el-button type="primary" icon="el-icon-circle-plus" @click="intDate">重置条件</el-button>
            </div>
            <el-table
                :data="productTable"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                width="100%"
                @selection-change="handleSelectionChange">

                <el-table-column type="selection" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="70%" align="center">
                    <template slot-scope="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column prop="categoryId" label="商品所属分类名" align="center">
                    <template slot-scope="scope">{{ scope.row.categoryName }}</template>
                </el-table-column>
                <el-table-column prop="productName" label="商品名称" align="center"></el-table-column>
                <el-table-column prop="price" label="商品价格" align="center">
                    <template slot-scope="scope">￥{{ scope.row.price }}</template>
                </el-table-column>
                <el-table-column prop="addTime" label="添加时间" align="center"></el-table-column>
                <el-table-column prop="updTime" label="最后更新时间" align="center"></el-table-column>
                <el-table-column label="操作" width="220%" align="center">
                    <template slot-scope="scope">
                        <el-button icon="el-icon-plus" @click="rightPop(scope.row.id)" type="text"
                                   style="margin-left: 16px;">
                            添加销量
                        </el-button>
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.row.id)"
                        >编辑
                        </el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row.id)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!--         添加弹窗 -->
        <el-dialog title="添加商品" :visible.sync="addVisible" width="30%">
            <el-form :model="product">
                <el-form-item label="所属分类">
                    <el-select v-model="product.categoryId" placeholder="请选择">
                        <el-option v-for="val in selectName" :key="val.id" :value="val.id" :label="val.categoryName" />
                    </el-select>
                </el-form-item>
                <el-form-item label="商品名称">
                    <el-input v-model="product.productName" style="width: 50%"></el-input>
                </el-form-item>

                <el-form-item label="商品价格">
                    <el-input type="number" v-model="product.price" style="width: 50%"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel()">取 消</el-button>
                <el-button type="primary" @click="saveProduct()">确 定</el-button>
            </span>
        </el-dialog>
        <!--添加销量弹窗-->
        <el-drawer

            :visible.sync="drawer"
            :with-header="false" size="25%">
            <div style="margin-top: 10%">
                <div style="margin-left: 45%">
                    <h3 style="color: #606266">{{ rightTitle }}</h3>
                </div>

                <el-form status-icon :model="rightForm" ref="ruleForm" label-width="25%" class="demo-ruleForm"
                         style="margin-left: 8%;margin-top: 10%">

                    <el-form-item label="生产数量" prop="pass" style="margin-top: 6%">
                        <el-input ref="mark" @keyup.enter.native="saveRightForm" @keyup.esc.native="saveRightForm"
                                  type="text" v-model="rightForm.productionNum" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="售出数量" prop="pass" style="margin-top: 6%">
                        <el-input @keyup.enter.native="saveRightForm" @keyup.esc.native="saveRightForm" type="text"
                                  v-model="rightForm.salesNum" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="报废数量" prop="pass" style="margin-top: 6%">
                        <el-input @keyup.enter.native="saveRightForm" @keyup.esc.native="saveRightForm" type="text"
                                  v-model="rightForm.failedNum" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="试吃数量" prop="pass" style="margin-top: 6%">
                        <el-input @keyup.enter.native="saveRightForm" @keyup.esc.native="saveRightForm" type="text"
                                  v-model="rightForm.testEatNum" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="赠送数量" prop="pass" style="margin-top: 6%">
                        <el-input @keyup.enter.native="saveRightForm" @keyup.esc.native="saveRightForm" type="text"
                                  v-model="rightForm.giveNum" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="pass" style="margin-top: 6%">
                        <el-input @keyup.enter.native="saveRightForm" @keyup.esc.native="saveRightForm" type="text"
                                  v-model="rightForm.remark" autocomplete="off"
                                  style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="销量时间" @keyup.enter.native="saveRightForm">
                        <el-date-picker
                            type="date"
                            placeholder="选择日期"
                            v-model="rightForm.date"
                            value-format="yyyy-MM-dd"
                            style="width: 80%;">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item style="margin-left: 12%;margin-top: 10%">
                        <el-button @click="next(rightForm.productId)">下一个</el-button>
                        <el-button @click="cancelRightFrom">取消</el-button>
                        <el-button type="primary" @click="saveRightForm">提交</el-button>
                    </el-form-item>
                </el-form>
            </div>

        </el-drawer>

    </div>
</template>

<script>
import { getLisProductCategoryV, getLisProductCategory, addUpdateProductCategory } from '@/api/productCategory';
import { getAllProductId, delProduct, getProductInfo, addUpdateProduct, getLisProduct } from '@/api/product';
import { addUpdateProductRecode } from '@/api/productRecode';


export default {
    name: 'basetable',
    data() {
        return {
            rightForm: {
                date: new Date()
            },
            value: '',
            drawer: false,
            rightTitle: '',
            initDate: {
                pageIndex: 1,
                pageSize: 10
            },
            query: {
                pageIndex: 1,
                pageSize: 10
            }, //查询条件
            productTable: [],
            multipleSelection: [], //多选参数
            addVisible: false,
            pageTotal: 0,
            product: {},
            ids: {
                ids: []
            },
            selectName: [],
            selectList: {
                pageNum: 1,
                pageSize: 99
            },
            rightFormArray: [],
            rightFormArrayV: []

        };
    },
    created() {
        this.getData();
    },
    methods: {
        //保存右弹窗数据
        saveRightForm() {
            console.log(this.rightForm);
            addUpdateProductRecode(this.rightForm).then(res => {
                if (res.success) {
                    // this.cancelRightFrom();
                    let s = this.rightFormArray.indexOf(this.rightForm.productId);
                    console.log(s);
                    console.log(this.rightFormArray.length - 1);
                    if (s !== this.rightFormArray.length - 1) {
                        this.next(this.rightForm.productId);
                        this.$message.success('添加成功');
                        this.rightForm = {
                            date: this.rightForm.date
                        };
                        this.$refs['mark'].focus();
                    } else {
                        this.$message.success('添加成功最后一个');
                        this.rightForm = {
                            date: new Date()
                        };
                        this.drawer = false;
                    }


                } else {
                    this.$message.warning(res.message);
                }

            });

        },

        cancelRightFrom() {
            this.drawer = false;
            this.rightForm = {
                date: new Date()
            };

        },

        //弹出侧边框
        rightPop(productId) {
            console.log('xiedai==' + productId);
            //获取所有商品id
            getAllProductId().then(res => {
                let arr = this.rightFormArray = [];
                res.data.forEach(function(item) {
                    arr.push(item.id);
                });
                this.rightFormArray = arr;
                console.log(this.rightFormArray);
            });
            this.drawer = true;
            //获取产品名称
            getProductInfo(productId).then(res => {
                this.rightTitle = res.data.productName;
                this.rightForm.productId = productId;
            });

        },
        next(productId) {
            console.log('old' + productId);
            let arr = this.rightFormArray;
            let index = arr.indexOf(productId);

            let pid = arr[index + 1];
            console.log('pid===' + pid);
            if (pid === undefined) {
                this.$message.warning('已经最后一个了！');
                return;
            }
            console.log('pid==' + pid);
            console.log('new' + pid);

            getProductInfo(pid).then(res => {
                this.rightForm.productId = pid;
                this.rightTitle = res.data.productName;
                console.log(this.rightTitle);
            });
            console.log(pid);

        },

        //选择框改变事件
        selectCategory(id) {
            this.query.categoryId = id;
            this.query.pageIndex = 1;
            this.getData();

        },

        //选择框点击事件
        getDivision() {
            getLisProductCategoryV(this.selectList).then(res => {
                console.log(res.data);
                this.selectName = res.data.records;
            });
        },

        //编辑弹窗
        handleEdit(id) {
            getLisProductCategoryV(this.selectList).then(res => {
                console.log(res.data);
                this.selectName = res.data.records;
            });
            getProductInfo(id).then(res => {
                if (res.success) {
                    this.addVisible = true;
                    this.product = res.data;
                } else {
                    this.$message.error(res.message);

                }
            });

        },

        //保存
        saveProduct() {

            addUpdateProduct(this.product).then(res => {
                console.log(res);
                if (res.success) {
                    this.$message.success('添加成功');
                    this.addVisible = false;
                    this.getData();
                } else {
                    this.$message.warning(res.message);
                }
            });


        },
        //重置条件
        intDate() {
            this.value = '';
            this.query = {};
            this.query.pageIndex = 1;
            this.query.pageSize = 10;
            this.getData();

        },

        //打开添加弹窗
        handleAdd() {
            this.product = {};
            this.addVisible = true;
            getLisProductCategoryV(this.selectList).then(res => {
                console.log(res.data);
                this.selectName = res.data.records;
            });

        },
        // 获取列表
        getData() {
            getLisProduct(this.query).then(res => {
                if (res.success) {
                    this.productTable = res.data.records;
                    this.pageTotal = res.data.total || 0;
                } else {
                    this.$message.warning(res.message);
                    if (res.code === 10005) {
                        this.$router.push({ path: '/login' });
                    }
                }


            });
        },
        //取消按钮
        cancel() {
            this.addVisible = false;
            //清空对象
            this.product = {};
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(id) {
            if (id !== undefined) {
                this.ids.ids = [];
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.ids.ids.push(id);
                delProduct(this.ids).then(res => {
                    if (res.success) {
                        this.$message.success('删除成功');
                        this.getData();
                    } else {
                        this.$message.error(res.message);
                    }

                });
            }).catch(() => {
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //多选删除
        delAllSelection() {
            for (let i = 0; i < this.multipleSelection.length; i++) {
                this.ids.ids.push(this.multipleSelection[i].id);
            }
            this.handleDelete();

        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>

.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.everyDay {
    display: inline-block;
    width: 20px;
    height: 20px;
    background-color: #409eff;
    color: #fff;
    border-radius: 50%;
}
</style>
